
var bsurl = require('../../../utils/baseUrl.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    noAddr: false,
    address: [],
    index: 0,
    lastDefault:-1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  onShow: function () {
    var that = this;
    wx.showToast({
      title: '加载中...',
      icon: "loading",
      duration: 150000
    })
    wx.request({
      url: bsurl + "buyer/queryAddress.do",
      method: "post",
      data: {

      },
      header: {
        'content-type': 'application/json', // 默认值
        JSESSIONID: app.globalData.JSESSIONID
      },
      success: function (res) {
        wx.hideToast();
        if (res.statusCode != 200) {
          wx.showModal({
            title: '提示',
            content: "请求失败",
          });
          return;
        }
        console.log(res);
        if (res.data.success) {
          that.setData({
            address: res.data.body,
            noAddr: false
          });
          for (var i in res.data.body){
            if (res.data.body[i].isDefault){
              that.setData({lastDefault:Number(i)});
              wx.setStorageSync("address", res.data.body[i]);
              break;
            }
          }
        }
        else {
          that.setData({
            noAddr: true
          });
          wx.showModal({
            title: '提示',
            content: res.data.message,
          });
        }
      },
      fail: function (res) {
        wx.hideToast();
        wx.showModal({
          title: '提示',
          content: "网络请求失败",
        });
      }
    })
  },
  addAddress: function () {
    wx.navigateTo({
      url: 'add/add?index=-1'
    })
  },
  //删除按钮
  del:function(e){
    console.log("del");
    var that = this;
    var index = e.currentTarget.dataset.index;
    wx.showModal({
      title: '提示',
      content: '确定删除该地址',
      success:function(res){
        if (res.confirm) {
          wx.showToast({
            title: '删除中...',
            icon: "loading",
            duration: 150000
          })
          wx.request({
            url: bsurl + "buyer/deleteAddress.do",
            method: "post",
            data: {
              body: {
                id: that.data.address[index].id
              }
            },
            header: {
              'content-type': 'application/json', // 默认值
              JSESSIONID: app.globalData.JSESSIONID
            },
            success: function (res) {
              wx.hideToast();
              if (res.statusCode != 200) {
                wx.showModal({
                  title: '提示',
                  content: "请求失败",
                });
                return;
              }
              console.log(res);
              if (res.data.success) {
                  that.data.address.splice(index,1);
                  that.setData({
                    address:that.data.address
                  })
              }
              else {
                wx.showModal({
                  title: '提示',
                  content: res.data.message,
                });
              }
            },
            fail: function (res) {
              wx.hideToast();
              wx.showModal({
                title: '提示',
                content: "网络请求失败",
              });
            }
          })
        }
      }
    })

  },
  //编辑按钮
  edit:function(e){
    
    var index = e.currentTarget.dataset.index;
    var addr = this.data.address[index];
    wx.navigateTo({
      url: 'add/add?id=' + addr.id + '&recname=' + addr.recname + '&phone=' + addr.phone + '&addr=' + addr.addr
    })
  },
  //设为默认按钮
  bindCheckbox:function(e){
    var index = e.currentTarget.dataset.index;
    var addr = this.data.address[index];
    if(addr.isDefault){
      return;
    }

    var that = this;
    wx.showToast({
      title: '加载中...',
      icon: "loading",
      duration: 150000
    })
    addr.isDefault = true;
    wx.request({
      url: bsurl + "buyer/updateAddress.do",
      method: "post",
      data: {
        body: addr
      },
      header: {
        'content-type': 'application/json', // 默认值
        JSESSIONID: app.globalData.JSESSIONID
      },
      success: function (res) {
        wx.hideToast();
        if (res.statusCode != 200) {
          wx.showModal({
            title: '提示',
            content: "请求失败",
          });
          return;
        }
        console.log(res);
        if (res.data.success) {
          that.data.address[index].isDefault = true;
          if (that.data.lastDefault > -1) {
            that.data.address[that.data.lastDefault].isDefault = false;
          }
          that.setData({
            address:that.data.address,
            lastDefault:index
          })
        }
        else {
          wx.showModal({
            title: '提示',
            content: res.data.message,
          });
        }
      },
      fail: function (res) {
        wx.hideToast();
        wx.showModal({
          title: '提示',
          content: "网络请求失败",
        });
      }
    })

    
    
  }
})